﻿using T0yK4T.Configuration;

namespace ToyChat.BuiltinModules
{
    /// <summary>
    /// This class is used clientside in <see cref="Core"/> to provide access to user permissions
    /// </summary>
    public class ClientPermissionsHandler : IPermissionsHandler
    {
        private Core core;
        private static readonly ConfigurableValue<ushort> PermissionsRequestTimeout = Config.GetValue<ushort>("getpermissionstimeout", new UInt16Converter(), 5000);

        /// <summary>
        /// Initializes a new instance of <see cref="ClientPermissionsHandler"/>
        /// </summary>
        /// <param name="coreModule">The <see cref="Core"/> that owns this handler</param>
        public ClientPermissionsHandler(Core coreModule)
        {
            this.core = coreModule;
        }

        /// <summary>
        /// Checks the permissions of the user with the specified UserID on the channel with the specfieid ChannelID for the specified Permissions
        /// </summary>
        /// <param name="username">The username of the user to check permissions for</param>
        /// <param name="channelID">The ID of the channel to check permissions for</param>
        /// <param name="permissions">The permissions to check for</param>
        /// <returns>True if the user has the specified permissions, if an error occurs the exception that was caught is rethrown (this is usually a <see cref="System.TimeoutException"/></returns>
        public bool CheckPermissions(string username, int channelID, ChannelPermissionFlags permissions)
        {
            try { return (this.GetPermissions(username, channelID).Permissions & permissions) == permissions; }
            catch { throw; }
        }

        /// <summary>
        /// Attempts to get the permissions for the user with the specified UserID on the channel with the specified ChannelID
        /// </summary>
        /// <param name="username">The username of the user to get permissions for</param>
        /// <param name="channelID">The ID of the channel to get permissions for</param>
        /// <returns>An <see cref="UserPermissions"/> object containing the permissions of the user if succesful, otherwise a TimeoutException is thrown</returns>
        public UserPermissions GetPermissions(string username, int channelID)
        {
            try { return this.core.RequestPermissions(username, channelID, PermissionsRequestTimeout); }
            catch { throw; }
        }

        /// <summary>
        /// Not implemented
        /// </summary>
        /// <param name="permissions"></param>
        public void StorePermissions(DefaultChannelPermissions permissions)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Not implemented
        /// </summary>
        /// <param name="permissions"></param>
        public void StorePermissions(UserPermissions permissions)
        {
            throw new System.NotImplementedException();
        }
    }
}